package com.weipai.demo.controller;

import com.weipai.demo.enums.RetCode;
import com.weipai.demo.mapper.base.UploadImgMapper;
import com.weipai.demo.model.LoansNews;
import com.weipai.demo.model.UploadImg;
import com.weipai.demo.service.FileUploadService;
import com.weipai.demo.service.LoansnewsService;
import com.weipai.demo.util.LayuiTablePagingResult;
import com.weipai.demo.util.Response;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;

/**
 * @Author: 赵项阳
 * @Date: 2019/6/21 18:34
 * @Version 1.0
 */
//咨讯
@RestController
public class LoansnewsController {

    @Autowired
    private LoansnewsService loansnewsService;
    @Autowired
    private FileUploadService fileUploadService;
    @Autowired
    private UploadImgMapper uploadImgMapper;

    //发布或修改文章
     @RequestMapping(value = "/loansNews/save")
     public Response save(LoansNews loansNews){
         return loansnewsService.save(loansNews);
     }


    //图片上传
    @RequestMapping(value = "/loansNews/uploadingImg", method = RequestMethod.POST)
    public Response uploadingImg(HttpServletRequest request, MultipartFile img) {
        Response response = new Response();
        UploadImg uploadImg=new UploadImg();
        if (img != null) {
            String result = fileUploadService.saveImgUpload(request, img);
            if (result.equals("formatError")) {
                response.setCode(RetCode.IMG_FORMAT);
            } else if (result.equals("sizeover")) {
                response.setCode(RetCode.IMG_SIZE);
            } else if (result.equals("error")) {
                response.setCode(RetCode.IMG_ERROR);
            } else {
                uploadImg.setImgUrl(result);
                int insert = uploadImgMapper.insert(uploadImg);
                response.put("result",result);
                response.setCode(RetCode.SUCCESS);
            }
        }else {
            response.setCode(RetCode.REQUEST_ERROR);
            return response;
        }
        return response;

    }


    //分页展示文章不包括内容
    @RequestMapping(value = "/loansNews/show")
    public LayuiTablePagingResult show(Integer page,Integer size,Integer news_class){
         return loansnewsService.show(page,size,news_class);
    }

    //首页推荐的文章
    @RequestMapping(value = "/loansNews/recommendNews")
    public LayuiTablePagingResult recommendNews(Integer page,Integer size){
         return loansnewsService.recommendNews(page,size);
    }


    //展示文章包含内容
    @RequestMapping(value = "/loansNews/showByContent")
    public Response showByContent(Integer id){
        return loansnewsService.showByContent(id);
    }

    //根据id删除文章
    @RequestMapping(value = "/loansNews/deleteById")
    public Response deleteById(Integer id){
        return loansnewsService.deleteById(id);
    }


}
